package com.jc.business.order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jc.business.base.BaseBusiness;
import com.jc.common.util.Base64Util;
import com.jc.common.util.ExceptionUtil;
import com.jc.common.util.HttpUtil;
import com.jc.common.util.MD5;
import com.jc.ds.model.Xorder;
import com.jc.framework.enums.Conclusion;
import com.jc.framework.enums.LogType;
import com.jc.framework.enums.OrderStatus;
import com.jc.service.model.jg.RequestAddOrder;
import com.jc.service.model.jg.RequestBaseModel;
import com.jc.service.order.XorderService;
import com.jc.service.systemloglist.SystemLogListService;

@Service
public class PushOrderBusiness extends BaseBusiness {
	
	@Autowired
	SystemLogListService systemLogListService;
	@Autowired
	XorderService xorderService;
	
	public boolean pushOrderToJG(RequestAddOrder order) {
		String result = "";		
		String appSecret = "B18DDB2FD2A76F88EC58B1E300D28C11";
		String appKey="8db256897d887f55";
		String url = "http://api.test.jioao.cn/gy_api2/apiOrder/addOrderObjectMall";
		String dataJson = JSONObject.toJSONString(order);
		String data = Base64Util.encode(dataJson);
		String sign = MD5.passmd5(data + appSecret).toUpperCase();
		RequestBaseModel requestBaseModel = new RequestBaseModel();
		requestBaseModel.setAppKey(appKey);
		requestBaseModel.setData(data);
		requestBaseModel.setSign(sign);
		String body = JSON.toJSONString(requestBaseModel);
		getLOGGER().error("请求地址:  " + url);
		getLOGGER().error("请求业务参数:  " + dataJson);
		getLOGGER().error("请求参数:  "+ body);
		
		try {
			result = HttpUtil.postJson(url, body, null);
			JSONObject returnObject = JSONObject.parseObject(result);
			if(returnObject != null && returnObject.getInteger("code") == 10000) {
				Xorder xorder = new Xorder();
				xorder.setId(order.getSelfOrderId());
				xorder.setOrderStatus(OrderStatus.push_ok.getName());
				xorderService.modify(xorder, false);
				systemLogListService.addSystemLog(body, Conclusion.OK, null, LogType.PUSH_JG_ORDER, result);
			}else {
				Xorder xorder = new Xorder();
				xorder.setId(order.getSelfOrderId());
				xorder.setOrderStatus(OrderStatus.push_finl.getName());
				xorderService.modify(xorder, false);
				systemLogListService.addSystemLog(body, Conclusion.FAIL, null, LogType.PUSH_JG_ORDER, result);
			}
		}catch (Exception e) {
			Xorder xorder = new Xorder();
			xorder.setId(order.getSelfOrderId());
			xorder.setOrderStatus(OrderStatus.push_finl.getName());
			xorderService.modify(xorder, false);
			systemLogListService.addSystemLog(body, Conclusion.FAIL, ExceptionUtil.subExceptionMessage(e, 0, 150), LogType.PUSH_JG_ORDER, result);
		}
		
		return false;
	}
	
	
	
//	public void pushLogisticsToH5(String channelOrderId,String ) {
//		
//		
//		
//		
//		
//		
//		
//		
//	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
